package com.lanchewuyou.user.driver.lipeiui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.lanchewuyou.user.driver.R;
import com.lanchewuyou.user.driver.modul.Config;
import com.lanchewuyou.user.driver.modul.OkHttpClientManager;
import com.squareup.okhttp.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * 还车开始页面
 */

public class PickCarActivity extends AppCompatActivity implements View.OnClickListener {
    private Toolbar mToolbar;
    private TextView pickPhone, pickOwner, pickCaseType, pickAdd, pickTime, pickMarked, pickPayType;
    private Button pickIknow, pickNextBtn;
    private RelativeLayout callPhone;
    private RelativeLayout pickMap;
    private String customPhone;
    private String carNumber;
    public static String orderId;
    private ProgressDialog progressDialog, progressUploadDialog;
    private static String DRIVERID;

    private Toast toast = null;

    private void showTextToast(String msg) {
        if (toast == null) {
            toast = Toast.makeText(PickCarActivity.this, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_car);
        initViews();
        SharedPreferences sharedPreferences = getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
        DRIVERID = sharedPreferences.getString("driverid", "");
        progressDialog.show();
        Intent intent = getIntent();
        String driverId = intent.getStringExtra("driverid");
        String caseId = intent.getStringExtra("caseid");
        orderId = caseId;
        OkHttpClientManager.getAsyn(Config.DRIVERORDERDETAIL + "&driverid=" + driverId + "&orderid=" + caseId, new OkHttpClientManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                showTextToast("没有获取到详情");
                progressDialog.dismiss();
            }

            @Override
            public void onResponse(String response) {
                Log.d("Pickcar", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString("status");
                    if (status.equals("1")) {
                        JSONObject info = jsonObject.getJSONObject("info");
                        customPhone = info.getString("ba_mobile");
                        pickPhone.setText(info.getString("username"));
                        pickOwner.setText(info.getString("chezhu_name"));
                        String accident = info.getString("accident");
                        if (accident.equals("1")) {
                            pickCaseType.setText("双车事故（第三者）");
                        } else if (accident.equals("2")) {
                            pickCaseType.setText("单车事故");
                        } else if (accident.equals("3")) {
                            pickCaseType.setText("双车事故");
                        }
                        pickAdd.setText(info.getString("address"));
                        pickTime.setText(info.getString("jieche_time"));
                        String paid = info.getString("paid");
                        if (paid.equals("0")) {
                            pickPayType.setText("线下付款");
                        } else if (paid.equals("1")) {
                            pickPayType.setText("已收费");
                        }
                        pickMarked.setText(info.getString("remark"));
                        carNumber = info.getString("chepai");
                        progressDialog.dismiss();
                    } else {
                        showTextToast("没有获取到详情");
                        progressDialog.dismiss();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    progressDialog.dismiss();
                }
            }
        });
    }

    private void initViews() {
        mToolbar = (Toolbar) findViewById(R.id.pick_car_toolbar);
        mToolbar.setTitle("信息确认");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        pickPhone = (TextView) findViewById(R.id.pick_car_num);
        pickOwner = (TextView) findViewById(R.id.pick_car_owner);
        pickCaseType = (TextView) findViewById(R.id.pick_case_type);
        pickAdd = (TextView) findViewById(R.id.pick_add);
        pickTime = (TextView) findViewById(R.id.pick_time);
        pickMarked = (TextView) findViewById(R.id.pick_marked);
        pickPayType = (TextView) findViewById(R.id.pick_pay_type);
        pickIknow = (Button) findViewById(R.id.pick_i_know);
        pickIknow.setOnClickListener(this);
        pickNextBtn = (Button) findViewById(R.id.pick_next_btn);
        pickNextBtn.setOnClickListener(this);
        callPhone = (RelativeLayout) findViewById(R.id.phone_relative);
        callPhone.setOnClickListener(this);
//        pickMap = (RelativeLayout) findViewById(R.id.pick_map_rela);
//        pickMap.setOnClickListener(this);
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("载入中");
        progressDialog.setMessage("请稍等");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(true);
        progressUploadDialog = new ProgressDialog(this);
        progressUploadDialog.setTitle("上传中");
        progressUploadDialog.setMessage("请稍等");
        progressUploadDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressUploadDialog.setCancelable(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.pick_map_rela:
//                if (NetUtils.isConnected(PickCarActivity.this)) {
//                    if (NetUtils.isGps(PickCarActivity.this)) {
//                        Intent intent2 = new Intent(PickCarActivity.this, PathActivity.class);
//                        startActivity(intent2);
//                    } else {
//                        new MaterialDialog.Builder(this)
//                                .title("是否进入GPS设置界面？")
//                                .content("您的GPS模块未打开，无法使用地图功能")
//                                .positiveText("确定")
//                                .negativeText("取消")
//                                .callback(new MaterialDialog.ButtonCallback() {
//                                    @Override
//                                    public void onPositive(MaterialDialog dialog) {
//                                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//                                        startActivityForResult(intent, 0);
//                                    }
//
//                                    @Override
//                                    public void onNegative(MaterialDialog dialog) {
//                                        super.onNegative(dialog);
//                                    }
//                                })
//                                .show();
//                    }
//                } else {
//                    showTextToast("网络未连接");
//                }
//
//                break;
            case R.id.pick_i_know:
                finish();
                break;
            case R.id.pick_next_btn:
                progressUploadDialog.show();
                OkHttpClientManager.getAsyn(Config.PUTCARINFOSTATUS + orderId + "&driverid=" + DRIVERID, new OkHttpClientManager.StringCallback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        showTextToast("服务器出了一点问题");
                        progressUploadDialog.dismiss();
                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String status = jsonObject.getString("status");
                            if (status.equals("1")) {
                                Intent intent1 = new Intent(PickCarActivity.this, CustomerInforActivity.class);
                                startActivity(intent1);
                                finish();
                                progressUploadDialog.dismiss();
                            } else {
                                progressUploadDialog.dismiss();
                                showTextToast("请求失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            progressUploadDialog.dismiss();
                        }

                    }
                });
                break;
            case R.id.phone_relative:
                Intent i = new Intent(Intent.ACTION_DIAL);
                i.setData(Uri.parse("tel:" + customPhone));
                startActivity(i);
            default:
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_pick_car, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            showBackDialog();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        showBackDialog();
    }

    private void showBackDialog() {
        new MaterialDialog.Builder(this)
                .title("退出任务")
                .content("点击确定将退出您正在执行的任务。")
                .positiveText("确定")
                .negativeText("取消")
                .callback(new MaterialDialog.ButtonCallback() {
                    @Override
                    public void onPositive(MaterialDialog dialog) {
                        finish();
                    }

                    @Override
                    public void onNegative(MaterialDialog dialog) {
                        super.onNegative(dialog);
                    }
                })
                .show();
    }
}
